package com.example.quickthumb;

import java.util.Arrays;

import com.quickthumb.game.Rat;
import com.quickthumb.game.RatLab;
import com.quickthumb.game.Screen;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceView;

public class LoadingView extends SurfaceView {

	public LoadingView(Context context) {
		super(context);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		Screen.setSize(w, h);

		hostGenerateRats();
		//getContext().startActivity(new Intent(getContext(), GameActivity.class));
		Intent j = new Intent(getContext(), GameActivity.class);
		j.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		getContext().startActivity(j);
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

	public void hostGenerateRats() {
		try {
			int d = 0;
			for (int i = 0; i < Rat.rats.size(); i++) {
				d += Rat.rats.get(i).percent();
				Log.d("pool", Rat.rats.get(i).percent() + " " + d);
			}
			Screen.pool_index = 0;

			for (int i = 0; i < Rat.rats.size(); i++) {
				for (int j = 0; j < (Screen.pool.length * Rat.rats.get(i).percent()) / d; j++)
					Screen.pool[(Screen.pool_index++) % Screen.pool.length] = Rat.rats.get(i).getClass().newInstance();
			}
			for (int i = 0; i < Screen.pool.length; i++)
				if (Screen.pool[i] == null)
					Screen.pool[i] = Rat.rats.get(0).getClass().newInstance();
			Log.d("size", Arrays.toString(Screen.pool));
		} catch (Exception e) {
			Log.d("exc", e.getMessage());
		}
	}
}